'use strict';

/* Controllers */

var main = angular.module('main', ['ngTable']);

main.controller('PhoneListCtrl', ['$scope', 'Phone',
  function($scope, Phone) {
    $scope.phones = Phone.query();
    $scope.orderProp = 'age';
  }]);

main.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone',
  function($scope, $routeParams, Phone) {
    $scope.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) {
      $scope.mainImageUrl = phone.images[0];
    });

    $scope.setImage = function(imageUrl) {
      $scope.mainImageUrl = imageUrl;
    }
  }]);


main.controller('urlController', 
  function ($scope, $http) {
    
    $http.get('http://filltext.com/?rows=999&id={index}&email={email}&username={username}&password={randomString|10}&fname={firstName}&lname={lastName}&address={streetAddress}').
        success(function(data) {
            $scope.users = data;
        });
});


main.controller('objController', function ($scope, $http) {
    /*this controller uses the config.params object as the request payload*/
    var config = {
        params: {

            
            'rows': 500,
            'avatar':'{image|10x10}',

            'fname': '{firstName}',
            'lname': '{lastName}',
            'address': '{streetAddress}',
            'callback': "JSON_CALLBACK"
        }
    }
    $http.jsonp("http://www.filltext.com", config, {}).success(function (data) {

        $scope.users = data
    });

});

main.controller('UserListCtrl', ['$scope', 'User', 'ngTableParams', '$filter', 
  function($scope, User, ngTableParams, $filter) {

    $scope.users = User.query();
    $scope.orderProp = 'fname';

    
    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10 ,          // count per page
        sorting: {
            fname: 'asc'     // initial sorting
        }
    }, {
        total: $scope.users.length, // length of data
        getData: function($defer, params) {
            var orderedData = params.sorting() ?
                                $filter('orderBy')($scope.users, params.orderBy()) :
                                $scope.users;
            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });

  }]);



